using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SideShow;
using SideShow.Scf;
using SideShow.Contents;

namespace PocketPCSideShowClient
{
    public partial class ApplicationList : UserControl
    {
        private List<SideShowApplication> _applications;

        public event EventHandler SelectedApplicationChanged;

        public ApplicationList()
        {
            InitializeComponent();
        }

        public List<SideShowApplication> Applications
        {
            get { return _applications; }
            set
            {
                _applications = value;
                BindData();
            }
        }

        public SideShowApplication SelectedApplication
        {
            get
            {
                if (lvApplications.SelectedIndices.Count == 1)
                {
                    return (SideShowApplication)lvApplications.Items[lvApplications.SelectedIndices[0]].Tag;
                }
                return null;
            }
        }

        private void BindData()
        {
            lvApplications.Items.Clear();
            imlSmallImages.Images.Clear();
            foreach (SideShowApplication app in _applications)
            {
                Util.MakeTransparent((Bitmap)app.SmallIcon, lvApplications.BackColor);
                imlSmallImages.Images.Add( app.SmallIcon);
                int i = imlSmallImages.Images.Count - 1;
                ListViewItem newItem = new ListViewItem(app.Name);
                newItem.ImageIndex = i;
                newItem.Tag = app;
                
                lvApplications.Items.Add(newItem);
                app.Endpoints[SideShowEndpoint.ScfFormatId].ContentAdded += new EventHandler<ContentEventArgs>(ApplicationListContentAdded);
            }
            columnHeader1.Width = lvApplications.ClientSize.Width;
        }

        delegate void MethodInvokerOneParam(object p);

        void ApplicationListContentAdded(object sender, ContentEventArgs e)
        {
            GlanceContent gc = e.Content as GlanceContent;
            if (InvokeRequired)
            {
                Invoke(new MethodInvokerOneParam(DoApplicationListContentAdded), gc);
            }
            else
            {
                DoApplicationListContentAdded(gc);
            }
        }

        private void DoApplicationListContentAdded(object param)
        {
            GlanceContent gc = (GlanceContent)param;
            if (gc != null && lvApplications.SelectedIndices.Count > 0)
            {
                SideShowApplication curApp = (SideShowApplication)lvApplications.Items[lvApplications.SelectedIndices[0]].Tag;
                if (curApp.Endpoints[SideShowEndpoint.ScfFormatId].ContentExists(gc))
                {
                    UpdateGlanceText(gc.Text);
                }
            }
        }

        private void UpdateGlanceText(object param)
        {
            string txt = (string)param;
            if (!string.IsNullOrEmpty(txt))
            {
                txt = txt.Replace("\n", "\r\n");
                txt = txt.Trim('\0');
            }
            txtGlanceText.Text = txt;
        }

        private void lvApplications_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvApplications.SelectedIndices.Count > 0)
            {
                SideShowApplication curApp = (SideShowApplication)lvApplications.Items[lvApplications.SelectedIndices[0]].Tag;
                GlanceContent gc = (GlanceContent)curApp.Endpoints[SideShowEndpoint.ScfFormatId].GetContent(0);
                if (gc != null)
                {
                    UpdateGlanceText(gc.Text);
                }
            }
        }

        private void lvApplications_Resize(object sender, EventArgs e)
        {
            columnHeader1.Width = lvApplications.ClientSize.Width;
        }

        private void lvApplications_ItemActivate(object sender, EventArgs e)
        {
            if (SelectedApplicationChanged != null)
            {
                SelectedApplicationChanged(this, e);
            }
        }
    }
}
